package model.crafting;

import model.inventory.item.Item;

public class EntityCraftingTable extends CraftingTable {
	protected RecipeComparer rc;

	public EntityCraftingTable(int w, int h, RecipeComparer rc) {
		super(w, h);
		this.rc = rc;
	}
	
	public Item addItem(int x, int y, Item item){
		Item temp = null;
		if(slots[x][y] != null && slots[x][y].getID() != item.getID())
			temp = slots[x][y];
		slots[x][y] = item;
		outSlot = rc.compare(this);
		return temp;
	}
	
	public Item removeItem(int x, int y){
		Item temp = slots[x][y];
		if(temp == null)
			return null;
		if(temp.getQuantity() > 1){
			temp.remove(1);
			return new Item(temp.getID(), 1);
		}
		slots[x][y] = null;
		outSlot = rc.compare(this);
		return temp;
	}
	
	public Item removeOutput(){
		if(outSlot != null){
			Item temp = outSlot;
			clearTable();
			return temp;
		}
		return null;
	}
	
	private void clearTable(){
		for(int y = 0; y < this.HEIGHT; y++){
			for(int x = 0; x < this.WIDTH; x++){
				slots[x][y] = null;
			}
		}
		outSlot = null;
	}

	public Item getItem(int x, int y) {
		return slots[x][y];
	}

}
